```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二叉树的Z字型遍历 - 算法可视化</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #2d3748;
            color: #e2e8f0;
            border-radius: 0.5rem;
            overflow-x: auto;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .step-card {
            border-left: 4px solid #667eea;
        }
        .zigzag-arrow {
            animation: bounce 2s infinite;
        }
        @keyframes bounce {
            0%, 100% { transform: translateX(0); }
            50% { transform: translateX(10px); }
        }
        .mermaid {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4">二叉树的Z字型遍历</h1>
                    <p class="text-xl md:text-2xl opacity-90 mb-6">探索算法之美，理解之字形遍历的精妙实现</p>
                    <div class="flex items-center">
                        <span class="bg-white text-purple-700 px-4 py-2 rounded-full font-medium mr-4">算法可视化</span>
                        <span class="flex items-center"><i class="fas fa-clock mr-2"></i> 阅读时间: 8分钟</span>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="relative">
                        <div class="bg-white bg-opacity-20 rounded-2xl p-6 backdrop-filter backdrop-blur-sm border border-white border-opacity-20">
                            <div class="text-center mb-4">
                                <div class="text-2xl font-bold mb-2">示例二叉树</div>
                                <div class="text-sm opacity-80">Z字型遍历顺序演示</div>
                            </div>
                            <div class="mermaid">
                                graph TD
                                A[1] --> B[2]
                                A --> C[3]
                                B --> D[4]
                                B --> E[5]
                                C --> F[6]
                                C --> G[7]
                            </div>
                            <div class="mt-4 text-center">
                                <div class="inline-flex items-center bg-white bg-opacity-20 px-3 py-1 rounded-full">
                                    <span class="mr-2">遍历顺序:</span>
                                    <span class="font-bold">1 → 3,2 → 4,5,6,7</span>
                                </div>
                            </div>
                        </div>
                        <div class="absolute -bottom-4 -right-4 bg-yellow-400 text-gray-800 px-3 py-1 rounded-full font-bold text-sm transform rotate-6 shadow-md">
                            <i class="fas fa-arrow-right zigzag-arrow mr-1"></i> 之字形
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-4xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-8 h-8 bg-purple-500 rounded-full flex items-center justify-center text-white mr-3">
                    <i class="fas fa-info"></i>
                </div>
                <h2 class="text-3xl font-bold">什么是Z字型遍历？</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                <p class="text-lg mb-4">
                    二叉树的Z字型遍历（也称为锯齿形遍历）是一种层次遍历的变体，它按照层级遍历的方式来访问二叉树的节点，但在遍历过程中会<strong>交替改变遍历的方向</strong>。
                </p>
                <p class="text-lg">
                    具体来说，第一层从左到右遍历，第二层从右到左遍历，第三层又从左到右遍历，依此类推。这种遍历方式可以揭示二叉树的不同视角，在某些应用场景中非常有用。
                </p>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                    <i class="fas fa-project-diagram"></i>
                </div>
                <h2 class="text-3xl font-bold">遍历过程可视化</h2>
            </div>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-gray-800 text-white px-4 py-3">
                        <h3 class="font-bold flex items-center">
                            <i class="fas fa-sitemap mr-2"></i> 二叉树结构
                        </h3>
                    </div>
                    <div class="p-4">
                        <div class="mermaid">
                            graph TD
                            A[1] --> B[2]
                            A --> C[3]
                            B --> D[4]
                            B --> E[5]
                            C --> F[6]
                            C --> G[7]
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-gray-800 text-white px-4 py-3">
                        <h3 class="font-bold flex items-center">
                            <i class="fas fa-random mr-2"></i> 遍历顺序动画
                        </h3>
                    </div>
                    <div class="p-4">
                        <div class="mermaid">
                            %%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#ffffff'}}}%%
                            gantt
                            title Z字型遍历顺序
                            dateFormat  X
                            axisFormat %s
                            section 第1层 (L→R)
                            1 : 0, 1
                            section 第2层 (R←L)
                            3 : 2, 3
                            2 : 3, 4
                            section 第3层 (L→R)
                            4 : 5, 6
                            5 : 6, 7
                            6 : 7, 8
                            7 : 8, 9
                        </div>
                        <div class="mt-4 text-sm text-gray-600">
                            <i class="fas fa-info-circle mr-1"></i> 注意箭头方向表示遍历方向的变化
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Algorithm Steps -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-8 h-8 bg-green-500 rounded-full flex items-center justify-center text-white mr-3">
                    <i class="fas fa-list-ol"></i>
                </div>
                <h2 class="text-3xl font-bold">Z字型遍历算法步骤</h2>
            </div>
            
            <div class="space-y-4">
                <div class="bg-white p-6 rounded-lg shadow-md step-card card-hover">
                    <h3 class="font-bold text-xl mb-3 flex items-center">
                        <span class="bg-purple-500 text-white rounded-full w-6 h-6 flex items-center justify-center mr-3 text-sm">1</span>
                        使用双端队列（Deque）
                    </h3>
                    <p>由于需要在不同层之间灵活地从前端或后端访问节点，双端队列（Deque）是一个理想的数据结构。它允许我们在两端进行插入和删除操作。</p>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-md step-card card-hover">
                    <h3 class="font-bold text-xl mb-3 flex items-center">
                        <span class="bg-purple-500 text-white rounded-full w-6 h-6 flex items-center justify-center mr-3 text-sm">2</span>
                        层级遍历
                    </h3>
                    <p>我们会像层次遍历一样，按层次逐层遍历二叉树，但根据层数的奇偶性来决定本层的访问方向（从左到右或从右到左）。</p>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-md step-card card-hover">
                    <h3 class="font-bold text-xl mb-3 flex items-center">
                        <span class="bg-purple-500 text-white rounded-full w-6 h-6 flex items-center justify-center mr-3 text-sm">3</span>
                        控制遍历方向
                    </h3>
                    <p>在遍历过程中，我们可以使用一个布尔值（或简单地检查当前层数的奇偶性）来决定在当前层是从左往右还是从右往左。</p>
                </div>
            </div>
        </section>

        <!-- Detailed Steps -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <div class="w-8 h-8 bg-orange-500 rounded-full flex items-center justify-center text-white mr-3">
                    <i class="fas fa-code"></i>
                </div>
                详细算法步骤
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                <div class="bg-gray-800 text-white px-4 py-3 flex justify-between items-center">
                    <h3 class="font-bold flex items-center">
                        <i class="fas fa-cogs mr-2"></i> 算法伪代码
                    </h3>
                    <span class="text-sm bg-white bg-opacity-20 px-2 py-1 rounded">Java实现</span>
                </div>
                <div class="p-4">
                    <div class="flex mb-4">
                        <div class="w-10 flex-shrink-0 text-gray-500 font-mono">1</div>
                        <div>初始化双端队列，根节点入队</div>
                    </div>
                    <div class="flex mb-4">
                        <div class="w-10 flex-shrink-0 text-gray-500 font-mono">2</div>
                        <div>创建布尔变量 leftToRight 记录遍历方向</div>
                    </div>
                    <div class="flex mb-4">
                        <div class="w-10 flex-shrink-0 text-gray-500 font-mono">3</div>
                        <div>当队列不为空时循环</div>
                    </div>
                    <div class="flex mb-4">
                        <div class="w-10 flex-shrink-0 text-gray-500 font-mono">4</div>
                        <div>记录当前层的节点数量 levelSize</div>
                    </div>
                    <div class="flex mb-4">
                        <div class="w-10 flex-shrink-0 text-gray-500 font-mono">5</div>
                        <div>根据 leftToRight 决定遍历方向</div>
                    </div>
                    <div class="flex mb-4">
                        <div class="w-10 flex-shrink-0 text-gray-500 font-mono">6</div>
                        <div>从左到右：从左端取节点，左子节点先入队</div>
                    </div>
                    <div class="flex mb-4">
                        <div class="w-10 flex-shrink-0 text-gray-500 font-mono">7</div>
                        <div>从右到左：从右端取节点，右子节点先入队</div>
                    </div>
                    <div class="flex">
                        <div class="w-10 flex-shrink-0 text-gray-500 font-mono">8</div>
                        <div>处理完当前层后切换方向</div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <div class="w-8 h-8 bg-indigo-500 rounded-full flex items-center justify-center text-white mr-3">
                    <i class="fas fa-laptop-code"></i>
                </div>
                完整代码实现
            </h2>
            
            <div class="code-block rounded-xl shadow-lg overflow-hidden card-hover">
                <div class="bg-gray-900 text-gray-300 px-4 py-3 flex justify-between items-center">
                    <div class="flex items-center">
                        <div class="w-3 h-3 bg-red-500 rounded-full mr-2"></div>
                        <div class="w-3 h-3 bg-yellow-500 rounded-full mr-2"></div>
                        <div class="w-3 h-3 bg-green-500 rounded-full"></div>
                    </div>
                    <span class="text-sm">BinaryTreeZigzagTraversalDemo.java</span>
                </div>
                <pre class="p-4 overflow-x-auto"><code class="language-java">import java.util.*;

public class BinaryTreeZigzagTraversalDemo {
    
    // 二叉树节点类
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        
        TreeNode(int val) {
            this.val = val;
        }
    }

    // Z字型遍历方法
    public static List&lt;List&lt;Integer&gt;&gt; zigzagLevelOrder(TreeNode root) {
        List&lt;List&lt;Integer&gt;&gt; result = new ArrayList&lt;&gt;(); // 存储结果
        if (root == null) return result; // 特殊情况处理：如果根节点为空，返回空结果
        
        Deque&lt;TreeNode&gt; deque = new LinkedList&lt;&gt;(); // 初始化双端队列
        deque.offer(root); // 根节点入队
        boolean leftToRight = true; // 标志变量，表示当前层的遍历方向
        
        while (!deque.isEmpty()) {
            int levelSize = deque.size(); // 当前层的节点数量
            List&lt;Integer&gt; currentLevel = new ArrayList&lt;&gt;(); // 用于存储当前层的节点值
            
            for (int i = 0; i &lt; levelSize; i++) {
                if (leftToRight) { // 从左到右遍历
                    TreeNode node = deque.pollFirst(); // 从左端取出节点
                    currentLevel.add(node.val); // 添加节点值到当前层结果
                    if (node.left != null) deque.offerLast(node.left); // 左子节点入队
                    if (node.right != null) deque.offerLast(node.right); // 右子节点入队
                } else { // 从右到左遍历
                    TreeNode node = deque.pollLast(); // 从右端取出节点
                    currentLevel.add(node.val); // 添加节点值到当前层结果
                    if (node.right != null) deque.offerFirst(node.right); // 右子节点入队
                    if (node.left != null) deque.offerFirst(node.left); // 左子节点入队
                }
            }
            
            result.add(currentLevel); // 将当前层结果添加到最终结果中
            leftToRight = !leftToRight; // 切换遍历方向
        }
        
        return result;
    }
    
    public static void main(String[] args) {
        // 创建一个二叉树
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        
        // 进行Z字型遍历
        List&lt;List&lt;Integer&gt;&gt; result = zigzagLevelOrder(root);
        
        // 输出结果
        for (List&lt;Integer&gt; level : result) {
            System.out.println(level);
        }
    }
}</code></pre>
            </div>
        </section>

        <!-- Output Example -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <div class="w-8 h-8 bg-pink-500 rounded-full flex items-center justify-center text-white mr-3">
                    <i class="fas fa-terminal"></i>
                </div>
                运行结果示例
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-gray-800 text-white px-4 py-3">
                        <h3 class="font-bold flex items-center">
                            <i class="fas fa-tree mr-2"></i> 输入二叉树结构
                        </h3>
                    </div>
                    <div class="p-4">
                        <div class="mermaid">
                            graph TD
                            A[1] --> B[2]
                            A --> C[3]
                            B --> D[4]
                            B --> E[5]
                            C --> F[6]
                            C --> G[7]
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="bg-gray-800 text-white px-4 py-3">
                        <h3 class="font-bold flex items-center">
                            <i class="fas fa-list-ol mr-2"></i> 程序输出结果
                        </h3>
                    </div>
                    <div class="p-4">
                        <div class="bg-gray-100 p-4 rounded-md">
                            <div class="font-mono text-gray-800">
                                <div class="mb-2">[1]</div>
                                <div class="mb-2">[3, 2]</div>
                                <div>[4, 5, 6, 7]</div>
                            </div>
                        </div>
                        <div class="mt-4 flex items-center text-sm text-gray-600">
                            <i class="fas fa-lightbulb mr-2 text-yellow-500"></i>
                            <span>输出展示了每一层的遍历结果，方向交替变化</span>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section class="bg-blue-50 rounded-xl p-6 mb-16 border border-blue-100">
            <h2 class="text-2xl font-bold mb-4 flex items-center text-blue-800">
                <i class="fas fa-star mr-3"></i> 关键要点总结
            </h2>
            <ul class="space-y-3">
                <li class="flex items-start">
                    <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                    <span><strong>双端队列是关键</strong> - 使用Deque可以灵活地从两端操作节点</span>
                </li>
                <li class="flex items-start">
                    <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                    <span><strong>方向交替变化</strong> - 通过布尔变量控制每一层的遍历方向</span>
                </li>
                <li class="flex items-start">
                    <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                    <span><strong>层次遍历基础</strong> - 在普通层次遍历基础上增加方向控制</span>
                </li>
                <li class="flex items-start">
                    <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                    <span><strong>时间复杂度O(n)</strong> - 每个节点只访问一次，效率与普通层次遍历相同</span>
                </li>
            </ul>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto px-4 max-w-6xl">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索算法与数据结构的艺术</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-500 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <!-- Mermaid JS -->
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            gantt: {
                axisFormat: '%H:%M'
            }
        });
    </script>
</body>
</html>
```